<?php
   if(!defined('ROOT_PATH')){
		include(NP_PATH.'public/404 Not Found.htm');
		exit();
	}
	
class Model{

	public $db='';
	protected $table;

	public function __Construct() {
	
		$this->db = mysql_connect(SQL_SERVER,SQL_USER,SQL_PWD);
		
		if (!$this->db) {
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db(SQL_DB);
		mysql_query("SET NAMES ".CHAR_SET);
		//echo 'Connected successfully';
		//mysql_close($this->db);

	}
	
//select field or * with where or not 
	public function select($field='',$where=''){

		$table = $this->table; $fields='';

		if($field!=''){
			$fields = implode(' , ' ,$field);
		}else{
			$fields.=' * ';
		}
		
		$sql= 'select '.$fields.' from '.TABLE_PRE.$table;
		if($where!=''){
			$sql.=" $where ";
		}

		return $this->query($sql);
	}
	
/*select field where limit()*/
	public function limit($field='',$where='',$order='',$limit=''){

		$table = $this->table; $fields='';

		if($field!=''){
			$fields = implode(' , ' ,$field);
		}else{
			$fields.=' * ';
		}
		
		$sql= 'select '.$fields.' from '.TABLE_PRE.$table;
		if($where!=''){
			$sql.=" $where ";
		}
		 
		if($order!=''){
			$sql.=" $order";
		}
		if($limit!=''){
			$sql.=" $limit";
		}
	//echo $sql; exit;
		return $this->query($sql);
	}
//update data and where
	public function update($data,$where=' where is null'){
	
		$fields=''; //$i=0;
		while (list($key, $val) = each($data)) {
			$f[] = "$key = '$val'";
			//$f[$i] = "$key = '$val'";
			//++$i;
		}
		
		$fields = implode(' , ' ,$f);
		$table = $this->table;
		$sql= 'UPDATE '.TABLE_PRE.$table.' SET '.$fields.' '.$where.' ';

		return  mysql_query($sql);
		
	}
	
//delete id = ? .
	public function delete($where='where is null'){
		$table = $this->table;
		$sql= 'DELETE FROM '.TABLE_PRE.$table.' '.$where;

		return mysql_query($sql);
		
	}
	
	public function insert($data){
		$i=0;
		while (list($key, $val) = each($data)) {
			$f[$i] = $key;
			$v[$i] = " '$val' ";
			++$i;
		}

		$fields = '( '. implode(' , ' ,$f) .' )';
		$values = '( '. implode(' , ' ,$v) .' )';

		$table = $this->table;
		$sql= 'INSERT INTO '.TABLE_PRE.$table.' '.$fields.' VALUES '.$values.' ';

		return mysql_query($sql);
		
	}
	
	public function set_table_name($table){
		$this->table = $table;
	}
	
	
	function query($sql){
	
		if(CACHE_FLAG===true){
			$cache = new Cache();
			$rs = $cache->getCache($sql);
			if($rs){
				return $rs;
			}
			else{
				$resource = mysql_query($sql);
				$result = $this->fetchAll($resource);
				$cache->setCache($sql,$result);
				return $result;
			}
		}else{
				$resource = mysql_query($sql);
				$result = $this->fetchAll($resource);
				return $result;
		}
	}
	
	function fetch($resource){
		return mysql_fetch_assoc($resource);
	}
	
	function fetchAll($resource){
		$rows = array();
		while($row = $this->fetch($resource))
		{
			$rows[] = $row;
		}
		return $rows;
	}
	
	function count($where=''){
		$table = $this->table;
		$sql= 'select count(*) as count FROM '.TABLE_PRE.$table.' '.$where;

		return $this->fetch(mysql_query($sql));
		
	}
	
	/*
	
	function findByid(){}
	function updateByid(){}
	*/
}


?>